﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="wrapper wrapper-content animated fadeInUp">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>数据还原</h5>
                    <div class="ibox-tools">
                        <a class="btn btn-info btn-xs p210" id="restore" action="RestoreData"><i class="fa fa-cubes fa-fw"></i> 还原数据</a>
                        <a class="btn btn-danger btn-xs p210" id="uploadbak" action="UploadBak"><i class="fa fa-cloud-upload fa-fw"></i> 上传数据库备份</a>
                        <a class="reload-link" style="color: #c4c4c4" href="javascript:dig.reload()" data-toggle="tooltip" data-placement="left" title="刷新">
                            <i class="fa fa-repeat fa-lg"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <div class="row">
                        <table id="dataTable" class="table table-striped table-bordered table-hover dataTables-example" style="text-align:center;">
                            <thead>
                                <tr>
                                    <th class="tn" style="width: 50px !important"></th>
                                    <th>备份名称</th>
                                    <th>备份类型</th>
                                    <th>备份大小</th>
                                    <th>备份时间</th>
                                </tr>
                            </thead>
                            <tbody></tbody>
                        </table>
                        <script id="tlist" type="text/x-jquery-tmpl">
                            <tr>
                                <td class="tn" style="width: 50px !important"><input name="check_files" type="checkbox" class="icheck_box" value="${path}" /></td>
                                <td>${name}</td>
                                <td>{{if ext==".zip" }}{{html '程序备份'}}{{else}}{{html '数据备份'}}{{/if}}</td>
                                <td>${size}</td>
                                <td>${time}</td>
                            </tr>
                        </script>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@section scripts{
    <script type="text/javascript" src="/Content/js/jquery.tmpl.js"></script>
    <script type="text/javascript">
        $(function () {
            ShowFiles();
            $("#restore").click(function () {
                var vals = '';
                var num = 0;
                $('input[name="check_files"]:checked').each(function () {
                    vals = $(this).val();
                    num++;
                });
                if (!vals) {
                    dig.error("对不起，请选中您要操作的文件！");
                    return;
                }
                if (num > 1) {
                    dig.error("对不起，只允许单个文件还原操作！");
                    return;
                }
                if (vals.substring(vals.lastIndexOf('.'), vals.length) == ".zip") {
                    dig.error("无法对程序文件进行在线还原！");
                    return;
                }
                var msg = "将进行数据还原，您确定吗？";
                dig.confirm("还原数据确认", msg, function () {
                    $.ajax({
                        type: "Post",
                        url: "/Com/BackupRestore/RestoreData",
                        data: {path:vals},
                        dataType: "json",
                        beforeSend: function () {
                            dig.loading("正在还原");
                        },
                        success: function (res) {
                            if (res.Status == "y") {
                                dig.successcallback(res.Msg, function () {
                                    window.location.reload();
                                });
                            } else {
                                dig.error(res.Msg);
                            }
                        },
                        error: function (e) {
                            dig.error("服务器忙，请稍后再试！");
                        }
                    });  
                });
            });
            $("#uploadbak").click(function () {
                dig.upload("/App_Data/BackUp/DataBaseBackUp/", function () {
                    var retval = this.returnValue;
                    if (retval != '') {
                        window.location.reload();
                    }
                });
            });
        });
        function ShowFiles()
        {
            $("#dataTable>tbody").html('<tr><td colspan="5" ><div class="sk-spinner sk-spinner-cube-grid"><div class="sk-cube"></div><div class="sk-cube"></div><div class="sk-cube"></div><div class="sk-cube"></div><div class="sk-cube"></div><div class="sk-cube"></div><div class="sk-cube"></div><div class="sk-cube"></div><div class="sk-cube"></div></div></td></tr>');
            $.post("/Com/BackupRestore/GetBackUpData", { fileExt: "" }, function (res) {
                if (res.Status == "y") {
                    if (res.Data == "" || res.Data == null) {
                        $("#dataTable>tbody").html('<tr><td colspan="5" class="alert alert-warning text-center"><span style="font-size:16px;"><i class="fa fa-warning"></i>&nbsp;没有找到任何文件</span></td></tr>');
                    } else {
                        $("#dataTable>tbody").empty();
                        $("#tlist").tmpl(res.Data).appendTo('#dataTable>tbody');
                        $(".icheck_box").iCheck({
                            checkboxClass: 'icheckbox_minimal-red',
                            radioClass: 'iradio_minimal-red',
                            increaseArea: '20%' // optional
                        });
                        //点击表格，选中/取消 复选框
                        $("#dataTable tr").slice(1).each(function (g) {
                            var p = $(this);
                            $(this).children().slice(1).click(function () {
                                if ($($(p.children()[0]).children()[0]).children().eq(0).prop("checked")) {
                                    $($(p.children()[0]).children()[0]).iCheck("uncheck");
                                }
                                else {
                                    $($(p.children()[0]).children()[0]).iCheck("check");
                                }
                            });
                        });
                    }
                } else if (res.Status == "empty") {
                    $("#dataTable>tbody").html('<tr><td colspan="5" class="alert alert-warning text-center"><span style="font-size:16px;"><i class="fa fa-warning"></i>&nbsp;没有找到任何文件</span></td></tr>');
                }
                else {
                    $("#dataTable>tbody").html('<tr><td colspan="5" class="alert alert-warning text-center"><span style="font-size:16px;"><i class="fa fa-warning"></i>&nbsp;' + res.Msg + '</span></td></tr>');
                }
            }, "json");
        }
    </script>
}

